//美国版首页
$(function(){
	home_index.run();
});

var home_index = {
		run: function(){
			this._init_();
			this._start_();
			this._init_events();				
		},
		
		_init_: function(){			
			this.currNum = 0;
			this.nextNum = 0;			
		},
		
		_start_: function(){				
			this._set_cookies();
			
		
			
			$(".img").hover(
				function(){
					$(this).children('div.tc_btn').fadeIn("slow");
				},
				function(){
					$(this).children('div.tc_btn').stop(true,false).fadeOut("fast");
				}
			);
			
			var aMonth = ["January","February","March","April","May","June","July","August","September","October","November", "December"];
			var now = new Date();			
			var mn = now.getMonth();
			var yy = now.getFullYear();
			$("#hSide_topBox > .tm_column > .title_movie > a").text(aMonth[mn]+",Movie "+yy);
		},		
		
		_set_cookies: function(){
			/*
			$.cookie("hl", "US", {
				expires: 360 * 24 * 3600
			});					
			
			if(this.ref){
				$.cookie("ref", this.ref, {
					expires: 360 * 24 * 3600
				});	
			}
			*/
		}, 
		
		_init_events: function(){	
			$("#movie_cont_div > div.m_box > div.img, " +
					"#tv_cont_div > div.m_box > div.img, " +
					"#music_cont_div > div.m_box > div.img").bind({
				mouseenter:function(){
					$(this).children('div.tc_btn').fadeIn("slow");
				},
				mouseleave:function(){
					$(this).children('div.tc_btn').stop(true,false).fadeOut("fast");
				}
			});			
			
			$("#hSearch_box").smenu({
				defaultValue : 'Search for Videos...',
				onchange : function(value) {
					value = encodeURIComponent(value);
					location.href = main.contextPath + "/results?search=" + value;
				}
			});
			
			$("#trailers_div > li > a ").click(function(){
				var type = $(this).attr("type");
				$("#trailers_div > li ").removeClass("curr");
				$(this).parent().addClass("curr");
				if(type == "in_theater"){
					$("#in_theater_div").show();
					$("#this_year_div").hide();
				}else{
					$("#in_theater_div").hide();
					$("#this_year_div").show();
				}
			});
			
			$(".hb_num > span").click(function(){
				var currNum = parseInt($(".hb_num > span.curr").text());
				var nextNum = parseInt($(this).text());
				//alert(currNum);
				//alert(nextNum);
				home_index.slide(currNum,nextNum);
				home_index.currNum = currNum + 1;
				
			});
			
			$("#hFooter_wrap > span.change_ver").click(function(){
				$("#hFooter_wrap > span.change_ver > ul").toggle();
			});
			$("#hFooter_wrap > span.change_ver").mouseover(function(){
				$("#hFooter_wrap > span.change_ver").css("cursor","pointer");
			});
			
			home_index._set_interval();
			
			this._compatible_mobilego();
			
			$(window).wresize(function(){
				home_index._compatible_mobilego();
			});
			
		},
		
		_compatible_mobilego: function(){
			var width = document.body.scrollWidth;
			if(width <= 980){
				$("#hm_side").hide();
				$("#his_box").hide();
				$("#fr_box").hide();
				
				$("#home_top > div.hTop_wrap").css("width","720");
				$("#home_top").css("overflow","hidden");
				
				$("#home_header > div.hHeader_wrap").css("width","720");
				$("#hSearch_box").css("width","500");
				
				$("#home_main").css("width","720");
				$("#hm_cont").css("float","none");
				$("#hm_cont").css("margin","0 auto");
				
				$("#bottom_nav > div.ni2").hide();
				$("#bottom_nav").css("width","720");
				
				$("#hFooter_wrap").css("width","720");
			}else{
				$("#hm_side").show();
				$("#his_box").show();
				$("#fr_box").show();
				
				$("#home_top > div.hTop_wrap").css("width","980");
				
				$("#home_header > div.hHeader_wrap").css("width","980");
				$("#hSearch_box").css("width","600");
				
				$("#home_main").css("width","980");
				$("#hm_cont").css("float","left");
				
				$("#bottom_nav > div.ni2").show();
				$("#bottom_nav").css("width","960");
				
				$("#hFooter_wrap").css("width","980");
			}
		},
		
		slide: function(currNum,nextNum){
			$page = $(".hb_num > span");
			$page.removeClass("curr");
			$page.eq(nextNum).addClass("curr");
			if(currNum == nextNum){
				return false;
			}else{
				$pic = $(".hb_img > li");
				$pic.eq(currNum).animate({'left':'-740px','z-index':'0'},{duration : 500});
				$pic.eq(nextNum).animate({'left':'0px','z-index':'5'},{duration : 500});
				$pic.eq(currNum).queue(function () {
			          $(this).css('left','740px');
			          $(this).dequeue();;
			      });
			}
			$title = $("#home_banner").find(".title");
			$title.hide();
			$title.eq(nextNum).fadeIn("slow");
			clearInterval(home_index.interval);
			home_index._set_interval();
		},
		
		_set_interval: function(){				
			home_index.interval = setInterval(function(){		
				if( home_index.currNum < 2){
					home_index.nextNum =  home_index.currNum + 1;
					home_index.slide(home_index.currNum,home_index.nextNum);
					home_index.currNum = home_index.currNum  + 1;
				}else{
					home_index.nextNum =  0;
					home_index.slide(home_index.currNum,home_index.nextNum);
					home_index.currNum = 0;
				}				
			}, 5000);			
		}		
};